package com.cion.shardingjdbcDemo.utils;

import java.util.Collection;

import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

public class TableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {



	@Override
	public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
		for(String item : availableTargetNames) {
			//因为算法生成的id都是偶数，示例两个分表的情况下匹配到的表都是0结尾，所以做两重求余
			if(item.endsWith((shardingValue.getValue() % 5) % 2 + "")) {
				return item;
			}
		}
		throw new UnsupportedOperationException("分表策略异常。");
	}

}
